<?php
class DB {

    private static $instance;
    private $MySQLi;

    // konstruktor nastavení k databázi
    private function __construct(array $dbOptions) {
        $this->MySQLi = @ new mysqli(	
            $dbOptions['db_host'],
            $dbOptions['db_user'],
            $dbOptions['db_pass'],
            $dbOptions['db_name']);

        if (mysqli_connect_errno()) {
            throw new Exception('Nefunkční databáze.');
        }
        $this->MySQLi->set_charset("utf8");
    }

    // připojení k databázi
    public static function init(array $dbOptions) {
        if (self::$instance instanceof self) {
            return false;
        }
        self::$instance = new self($dbOptions);
    }

    // Mysql dotaz
    public static function query($q) {
        return self::$instance->MySQLi->query($q);
    }

    // escape ochrana 
    public static function esc($str) {
        return self::$instance->MySQLi->real_escape_string(htmlspecialchars($str));
    }
}
?>